#!/bin/bash


echo "-***********************************-"
echo "超牛X ODS层用户行为日志表 行数统计脚本启动"
echo "-***********************************-"

dt=$(date -d'-1 day' +%Y-%m-%d)
cur_dt=$(date +%Y-%m-%d)

if [ $1 ]
then
dt=$1
fi

echo "统计的日期为: $dt"


# 检查几个重要字段的空值个数
# username,device_id,action_time
# 还要检查action_time字段的最大值和最小值
sql="
select '_x_flag_',
count(1),
sum(if(username is null,1,0)),
sum(if(device_id is null,1,0)),
sum(if(action_time is null,1,0)),
max(action_time),
min(action_time)
from ods.user_action_log
where dt='${dt}'
"




x=$(hive -e "$sql" | grep _x_flag_ | grep -v select)


# 行总数
line_amt=$(echo $x | cut -d' ' -f 2)

# username空值行数
username_null_amt=$(echo $x | cut -d' ' -f 3)

#device_id 空值行数
device_id_null_amt=$(echo $x | cut -d' ' -f 4)

#action_time 空值行数
action_time_null_amt=$(echo $x | cut -d' ' -f 5)

#action_time 最大值
action_time_max=$(echo $x | cut -d' ' -f 6)

#action_time 最小值
action_time_min=$(echo $x | cut -d' ' -f 7)



param="
{
  \"target_table\": \"ods.user_action_log\",
  \"target_date\": \"${dt}\",
  \"check_time\": \"${cur_dt}\",
  \"check_result\": {
    \"table_line_amt\": ${line_amt},
    \"field_check_results\": [
      {
        \"check_field\": \"username\",
        \"check_item\": \"null\",
        \"check_value\": ${username_null_amt}
      },
      {
        \"check_field\": \"device_id\",
        \"check_item\": \"null\",
        \"check_value\": ${device_id_null_amt}
      },
      {
        \"check_field\": \"action_time\",
        \"check_item\": \"null\",
        \"check_value\": ${action_time_null_amt}
      },
      {
        \"check_field\": \"action_time\",
        \"check_item\": \"max\",
        \"check_value\": ${action_time_max}
      },
      {
        \"check_field\": \"action_time\",
        \"check_item\": \"min\",
        \"check_value\": ${action_time_min}
      }
    ]
  }
}
"
echo $param

# 将质量检查结果上报给质量管理平台
curl -X POST -H 'Content-Type:application/json;utf-8' -H 'Accept:application/json' -d "$param" http://192.168.77.2:8080/api/single_table_dq_check
